package com.zhongxi.guoyuan.view.coinPostShot;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.coinPost.CoinEvaluateAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinPost.CoinPostGoodsData;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CoinEvaluateActivity extends BaseActivity {

    private PullToRefreshListView lvList;

    private List<CoinPostGoodsData> listGoods = new ArrayList<>();
    public static final int EVALUATE_RESULT_CODE = 222;
    private CoinEvaluateAdapter adapter;

    private Intent intent;
    private String orderid;
    private String shopid;
    private String mirrorInfo;

    private String type;
    private int position;
    private int score;
    private String content;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    position = msg.arg1;
                    score = msg.arg2;
                    content = (String) msg.obj;
                    if (content.length() == 0 || content.equals("")) {
                        showShortToast("Please write your evaluate!");
                    } else {
                        submitEvaluate();
                    }
                    break;
            }
        }
    };
    private int flag = 0;

    /**
     * 提交评价
     */
    private void submitEvaluate() {
        type = "evaluate";
        Map<String, String> map = new HashMap<>();
        map.put("orderid", orderid);
        map.put("shopid", shopid);
        map.put("goodsid", listGoods.get(position).getGoodsid());
        map.put("starlevel", score + "");
        map.put("mirrorid", listGoods.get(position).getId());
        map.put("content", content);
        if (flag == 1) {
            getServer(Url.REWARD_GO_COMMENT, map, " ", false, null, "post");
        } else {
            getServer(Url.POST_GO_COMMENT, map, " ", false, null, "post");
        }
    }

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_coin_evaluate);
        initTitle();
        tvTitle.setText("商品评价");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
    }

    @Override
    protected void initData() {
        intent = getIntent();
        orderid = intent.getStringExtra("orderid");
        shopid = intent.getStringExtra("shopid");
        mirrorInfo = intent.getStringExtra("mirror");
        dealWithMirror();
        flag = intent.getIntExtra("flag", 0);
        adapter = new CoinEvaluateAdapter(this, listGoods, handler);
        lvList.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    private void dealWithMirror() {
        JSONObject jsonMirr = null;
        try {
            jsonMirr = new JSONObject(mirrorInfo);
            JSONArray jsonArray = jsonMirr.optJSONArray(orderid);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject o = (JSONObject) jsonArray.get(i);
                CoinPostGoodsData goods = new CoinPostGoodsData();
                goods.setId(o.optString("id"));
                goods.setGoodsid(o.optString("goodsid"));
                goods.setGoodsname(o.optString("goodsname"));
                goods.setThumbnail(o.optString("thumbnail"));
                goods.setSubject(o.optString("subject"));
                goods.setShouldprice(o.optString("shouldprice"));
                goods.setGoodsunitprice(o.optString("goodsunitprice"));
                goods.setGoodsnum(Integer.parseInt(o.optString("goodsnum")));
                goods.setGattr(o.optString("gattr"));
                goods.setOrderid(o.optString("orderid"));
                goods.setPrice(o.optString("price"));
                goods.setShopid(o.optString("shopid"));
                goods.setUserid(o.optString("userid"));
                listGoods.add(goods);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
        showShortToast("提交成功");
        listGoods.remove(position);
        setResult(EVALUATE_RESULT_CODE);
        if (listGoods.size() == 0) {
            finish();
        } else {
            if (position == listGoods.size()) {
                CoinPostGoodsData coinPostGoodsData = listGoods.get(position - 1);
                coinPostGoodsData.setStatus(1);
            } else {
                CoinPostGoodsData coinPostGoodsData = listGoods.get(position);
                coinPostGoodsData.setStatus(1);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            showShortToast("失败" + result.getCode());
            if (1000 == result.getCode()) {
                showShortToast("等待");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
